<?php
/*********************************************************************\
*  Copyright (c) 1998-2013, TH. All Rights Reserved.
*  Author :wuyou
*  FName  :ProKind.php
*  Time   :2019/02/14 15:43:00
*  Remark :产品大类
\*********************************************************************/
FLEA::loadClass('TMIS_Controller');
class Controller_Jichu_ProKind extends TMIS_Controller {
    var $_modelExample;
    var $funcId = '90-7-1';
    var $funcEditId = '90-7-2';
    var $funcExtendId = '90-7-3';
    var $funcFuyongId = '90-7-4';
    var $funcPrintId = '95-7';
    function __construct() {
        $this->_modelExample = FLEA::getSingleton("Model_Jichu_ProKind");
        $this->_modelPrintConfig = FLEA::getSingleton("Model_Jichu_PrintConfig");
    }

    /**
     * 角色html配置form
     * Time：2018/12/13 15:49:40
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    public function buildHtml() {
        $params = array();
        $params['formItems'] = array(
            array(
                'type'      =>'comp-text',
                'name'      =>'kindName',
                'title'     =>'产品类别',
                'clearable' =>true,
                'value'     =>'',
            )
        );

        $params['rules'] = array(
            'kindName'=>array(
                array(
                    'required'=>true,
                    'message'=>'产品类别名称必须',
                )
            ),
        );

        return $params;
    }

    /**
     * @desc ：部门档案查询
     * Time：2017/07/31 15:33:05
     * @author li
    */
    function actionRight() {
        $this->authCheck($this->funcId);

        $searchItems = array(
            'key'        =>''
        );
        $smarty = & $this->_getView();
        $arrFieldInfo = array(
            "id"         =>array('text'=>"编号",'width'=>'120'),
            "kindName"   =>array('text'=>"产品类别名称",'width'=>''),
            "extendAttr" =>array('text'=>"扩展属性",'width'=>''),
        );

        if(!FLEA::getAppInf('setAttrBySelf')){
            unset($arrFieldInfo['extendAttr']);
        }

        $smarty->assign('title', '产品类别列表');
        $smarty->assign('arr_field_info', $arrFieldInfo);
        $smarty->assign('action', $this->_url('GetRows'));
        $smarty->assign('searchItems', $searchItems);
        $smarty->assign('colsForKey', array(
                array('text' =>'关键字','col'=>'key')
        ));
        $editButtons = array(
            array('text'=>'编辑','type'=>'redirect','icon'=>'el-icon-edit','options'=>array(
                //点击后跳转的地址
                'url'            =>$this->_url('Edit').'&id={id}',
                'disabledColumn' =>'__disabledEdit',
            )),
            array('text'=>'删除','type'=>'remove','options'=>array(
                'url'            =>$this->_url('RemoveAjax'),
                'disabledColumn' =>'__disabledRemove',
            )),
        );
        if(FLEA::getAppInf('setAttrBySelf')){
            $editButtons[] = array('text'=>'扩展属性：建产品数据后禁止调整','type'=>'redirect','icon'=>'el-icon-setting','options'=>array(
                //点击后跳转的地址
                'url'            =>$this->_url('SetFileds').'&id={id}',
                'disabledColumn' =>'__disabledSetting',
            ));
        }
        $editButtons[] = array('text'=>'复制分类','type'=>'func','icon'=>'el-icon-document-copy','options'=>array(
            'funcName'       =>"FuncCopy",
            'disabledColumn' =>'__disabledCopy',
        ));

        $smarty->assign('editButtons',$editButtons);
        $smarty->assign('addUrl',$this->_url('Add'));
        $smarty->assign('sonTpl',array(
            'Jichu/TableProductKindList.js'
        ));

        $smarty->display('TableList.tpl');
    }

    /**
     * 获取计划任务的数据
     * @author li
    */
    public function actionGetRows()
    {

        $auth = $this->authCheck(0 ,true);

        if(!$auth){
            $ret = array(
              'total'   =>0,
              'columns' =>array(),
              'rows'    =>array(),
            );
            echo json_encode($ret);exit;
        }

        $post = $this->axiosPost();

        $pagesize    = $post['pagesize'];
        $currentPage = $post['currentPage'];

        $keyField    = isset($post['colForKey']) ? $post['colForKey'] : 'key';
        //处理搜索
        $post[$keyField] = $post['key'];
        if($keyField!='key') $post['key'] = '';

        $arr = $post;

        $condition=array();
        if($arr['key']!='') {
            $condition[] = array('kindName',"%{$arr['key']}%",'like');
        }

        // dump($sql);exit;
        FLEA::loadClass('TMIS_Pager');
        $pager = & new TMIS_Pager($this->_modelExample,$condition,'id asc',$pagesize ,($currentPage - 1));
        $rowset = $pager->findAll();
        // dump($rowset);exit;
        $modelProduct = FLEA::getSingleton("Model_Jichu_Product");
        foreach($rowset as & $v){
            //如果已经有产品数据了，禁止修改属性值
            // $_tmpCount = $modelProduct->findCount(array('kindId'=>$v['id']));
            // $_tmpCount > 0 && $v['__disabledSetting'] = true;

            if($v['extendAttr']){
                $_tmp = json_decode($v['extendAttr'] ,1);
                $_tmpExt = array();
                foreach ($_tmp as & $f) {
                    if($f['title'] && $f['status'] == 'on'){
                        $_tmpExt[] = $f['title'];
                    }
                }
                $v['extendAttr'] = join(',',$_tmpExt);
            }

            //判断是否有配置打印标签的设置
            if($v['printConfig']){
                $_tmp = json_decode($v['printConfig'] ,1);
                if($_tmp['template']){
                    $v['printConfig'] = "已设置";
                }

                if($_tmp['template'] != 'qrcode'){
                    $v['__disabledSetPrint2'] = true;
                }
            }
        }

        $ret = array(
          'total'   =>$pager->totalCount,
          'columns' =>array(),
          'rows'    =>$rowset,
        );
        echo json_encode($ret);exit;
    }

    /**
     * 新增
     * Time：2018/12/17 13:35:37
     * @author li
    */
    function actionAdd() {
        $this->authCheck($this->funcEditId);
        $this->_edit();
    }

    /**
     * 编辑修改
     * Time：2018/12/17 13:36:35
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionEdit() {
        $this->authCheck($this->funcEditId);
        $arr = $this->_modelExample->find($_GET['id']);
        $this->_edit($arr);
    }

    function _edit($Arr = array()) {
        $formParams = $this->buildHtml();
        if(!$Arr){
            foreach ($formParams['formItems'] as $key => $v) {
                $Arr[$v['name']] = '';
            }
        }

        // dump($Arr);exit;

        $smarty = & $this->_getView();

        $smarty->assign('formItems',$formParams['formItems']);
        $smarty->assign('rules',$formParams['rules']);
        $smarty->assign('title','基本信息');
        $smarty->assign('row',$Arr);
        $smarty->assign('action',$this->_url('Save'));
        $smarty->assign('targetUrl',$this->_url('Right'));
        $smarty->display('MainForm.tpl');
    }

    /**
     * 编辑修改
     * Time：2018/12/17 13:36:35
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionFuyong($id) {
        $formParams = $this->buildHtml();
        $Arr = $this->_modelExample->find($id);
        if(!$Arr){
            foreach ($formParams['formItems'] as $key => $v) {
                $Arr[$v['name']] = '';
            }
        }

        // dump($Arr);exit;

        $smarty = & $this->_getView();

        $smarty->assign('formItems',$formParams['formItems']);
        $smarty->assign('rules',$formParams['rules']);
        $smarty->assign('title','基本信息');
        $smarty->assign('row',$Arr);
        $smarty->assign('action',$this->_url('Save'));
        $smarty->assign('targetUrl',$this->_url('Right'));
        $smarty->display('MainForm.tpl');
    }

    /**
     * 保存
     * Time：2018/12/17 13:41:52
     * @author li
    */
    function actionSave() {
        $this->authCheck($this->funcEditId);
        $post = $this->axiosPost();

        if(empty($post['id'])) {
            $condition = array();
            $condition['kindName'] = $post['kindName'];
            $count = $this->_modelExample->findCount($condition);

            if($count > 0) {
                $ret = array('msg'=>'产品类别名称重复','success'=>false);
                echo json_encode($ret);exit;
            }
        } else {
            //修改时判断是否重复
            $condition = array();
            $condition['kindName'] = $post['kindName'];
            $condition[] = array('id',$post['id'],'<>');
            $count = $this->_modelExample->findCount($condition);

            if($count > 0) {
                $ret = array('msg'=>'产品类别名称重复','success'=>false);
                echo json_encode($ret);exit;
            }
        }

        //数据保存
        unset($post['extendAttr']);
        unset($post['printConfig']);
        $result = $this->_modelExample->save($post);
        if($result){
            $ret = array(
                'success'   =>true,
                'msg'       =>'操作成功',
                'targetUrl' =>$this->_url('Right'),
            );
        }else{
            $ret = array(
                'success' =>false,
                'msg'     =>'操作失败',
            );
        }

        echo json_encode($ret);exit;
    }

    /**
     * @desc ：员工档案删除 验证是否已经被使用
     * Time：2017/07/31 15:49:06
     * @author lwj
    */
    function actionRemoveAjax() {
        $post = $this->axiosPost();
        $id = intval($post['row']['id']);

        //查找是否已经被使用
        $array = array(
            'Model_Jichu_Product' => 'kindId',
        );

        $isOccupy = false;

        foreach ($array as $key =>  & $v) {
            $tmpModel = FLEA::getSingleton($key);
            $condition = array();
            $condition[$v] = $id;
            $count = $tmpModel->findCount($condition);

            //如果某个表中存在，则不能删除
            if($count > 0){
                $isOccupy = true;
                $msg = $tmpModel->moduleName;
                break;
            }
        }

        if($isOccupy){
            $ret = array(
                'success' =>false,
                'msg'     =>'被'.$msg.'使用，禁止删除',
            );
            echo json_encode($ret);exit;
        }


        $res = $this->_modelExample->removeByPkv($id);

        if($res){
            $ret = array(
                'success' =>true,
                'msg'     =>'删除成功',
            );
        }else{
            $ret = array(
                'success' =>false,
                'msg'     =>'删除失败',
            );
        }
        echo json_encode($ret);exit;
    }

    /**
     * @desc ：员工档案删除 验证是否已经被使用
     * Time：2017/07/31 15:49:06
     * @author lwj
    */
    function actionRemovePrintConfigAjax() {
        $post = $this->axiosPost();
        $id = intval($post['row']['id']);
        //查找是否已经被使用
        $array = array(
            // 'Model_Jichu_Product' => 'kindId',
        );

        $isOccupy = false;

        foreach ($array as $key =>  & $v) {
            $tmpModel = FLEA::getSingleton($key);
            $condition = array();
            $condition[$v] = $id;
            $count = $tmpModel->findCount($condition);

            //如果某个表中存在，则不能删除
            if($count > 0){
                $isOccupy = true;
                $msg = $tmpModel->moduleName;
                break;
            }
        }

        if($isOccupy){
            $ret = array(
                'success' =>false,
                'msg'     =>'被'.$msg.'使用，禁止删除',
            );
            echo json_encode($ret);exit;
        }

        $res = $this->_modelPrintConfig->removeByPkv($id);
        if($res){
            $ret = array(
                'success' =>true,
                'msg'     =>'删除成功',
            );
        }else{
            $ret = array(
                'success' =>false,
                'msg'     =>'删除失败',
            );
        }
        echo json_encode($ret);exit;
    }


    /**
     * 角色html配置form
     * Time：2018/12/13 15:49:40
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    public function buildHtmlExtend() {
        $params = array();
        $params['mainFormItems'] = array(
            array(
                'type'        =>'comp-text',
                'name'        =>'kindName',
                'title'       =>'产品类别',
                'placeholder' =>'数据有误，请返回重试',
                'value'       =>'',
                'readonly'    =>true,
            )
        );

        $arrayType = array();
        foreach ($this->_modelExample->arrayType as $k => &$v) {
            if($k == 'autocomplete') $v .= "(字段值固定时)";
            $arrayType[] = array('text'=>$v ,'value'=>$k);
        }


        $params['sonFormItems'] = array(
            'status'=>array(
                'type'       =>'comp-select',
                'name'       =>'status',
                'title'      =>'是否启用',
                'value'      =>'',
                'clearable'  =>false,
                'options'    =>array(
                    array('text'=>'启用','value'=>'on'),
                    array('text'=>'关闭','value'=>'off'),
                ),
                'filterable' =>true,
                'multiple'   =>false,
                'disabled'   =>false,
            ),
            'title'=>array(
                'type'        =>'comp-text',
                'name'        =>'title',
                'title'       =>'字段名称',
                'append'      =>'如后整',
                'clearable'   =>true,
                'value'       =>'',
                'placeholder' =>'',
            ),
            'titleEng'=>array(
                'type'        =>'comp-text',
                'name'        =>'titleEng',
                'title'       =>'字段英文',
                'append'      =>'打印标签的时候会用',
                'clearable'   =>true,
                'value'       =>'',
                'placeholder' =>'',
            ),
            'type'=>array(
                'type'       =>'comp-select',
                'name'       =>'type',
                'title'      =>'字段类型',
                'value'      =>'',
                'clearable'  =>false,
                'options'    =>$arrayType,
                'filterable' =>true,
                'multiple'   =>false,
                'disabled'   =>false,
            ),
            'showType'=>array(
                'type'       =>'comp-select',
                'name'       =>'showType',
                'title'      =>'展示方式',
                'value'      =>'',
                'clearable'  =>false,
                'options'    =>array(
                    array('text'=>'默认','value'=>'default'),
                    array('text'=>'单列','value'=>'single'),
                ),
                'filterable' =>true,
                'multiple'   =>false,
                'disabled'   =>false,
            ),
            'showSort'=>array(
                'type'        =>'comp-text',
                'name'        =>'showSort',
                'title'       =>'单列显示顺序',
                'append'      =>'数字越大，显示越靠前',
                'clearable'   =>true,
                'value'       =>'0',
                'placeholder' =>'',
            ),
            'colWidth'=>array(
                'type'        =>'comp-text',
                'name'        =>'colWidth',
                'title'       =>'查询列表宽度',
                'append'      =>'默认为宽度1，支持一位小数',
                'clearable'   =>true,
                'value'       =>'1',
                'placeholder' =>'',
            ),
            'isRequire'=>array(
                'type'       =>'comp-select',
                'name'       =>'isRequire',
                'title'      =>'是否必填',
                'value'      =>'',
                'clearable'  =>false,
                'options'    =>array(
                    array('text'=>'是','value'=>'on'),
                    array('text'=>'否','value'=>'off'),
                ),
                'filterable' =>true,
                'multiple'   =>false,
                'disabled'   =>false,
            ),
        );

        $params['columnsSon'] = array(
            'edit'     =>array('text'=>'','width'=>'','showButton'=>true),
            'status'   =>array('text'=>'是否启用','width'=>'','summation'=>false,'displayKey'=>'statusDesc'),
            'title'    =>array('text'=>'字段名称','width'=>'','summation'=>false),
            'titleEng' =>array('text'=>'字段名称英文','width'=>'','summation'=>false),
            'type'     =>array('text'=>'字段类型','width'=>'','summation'=>false,'displayKey'=>'typeDesc'),
            'showType' =>array('text'=>'展示方式','width'=>'','summation'=>false,'displayKey'=>'showTypeDesc'),
            'showSort' =>array('text'=>'单列显示顺序','width'=>'','summation'=>false),
            'colWidth' =>array('text'=>'查询列表宽度','width'=>'','summation'=>false),
            'isRequire'=>array('text'=>'是否必填','width'=>'','summation'=>false,'displayKey'=>'requireDesc'),
        );

        $params['rules'] = array(

        );

        return $params;
    }

    /**
     * 设置字段
     * Time：2019/10/22 15:01:58
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionSetFileds(){
        $this->authCheck($this->funcExtendId);
        $arr = $this->_modelExample->find($_GET['id']);
        $formParams = $this->buildHtmlExtend();
        //扩展字段的值
        $arr['ExtendAttr'] = array();
        $arr['extendAttr'] && $arr['ExtendAttr'] = json_decode($arr['extendAttr'] ,true);
        // dump($arr);exit;

        //默认10行
        while (count($arr['ExtendAttr']) < 15) {
            $arr['ExtendAttr'][] = array(
                'type'     =>'text',
                'status'   =>'off',
                'title'    =>'',
                'titleEng' =>'',
                'showType' =>'default',
                'showSort' =>0,
                'colWidth' =>1,
            );
        }

        foreach ($arr['ExtendAttr'] as & $v) {
            $v['statusDesc'] = $v['status'] == "on" ? '启用' : '关闭';
            $v['requireDesc'] = $v['isRequire'] == "on" ? '是' : '否';
            $v['showTypeDesc'] = $v['showType'] == "default" ? '默认' : '单列';
            if($v['type']){
                $v['typeDesc'] = $this->_modelExample->arrayType[$v['type']];
            }
        }
        // dump($arr);exit;

        $smarty = & $this->_getView();

        $smarty->assign('sonFormItems',$formParams['sonFormItems']);
        $smarty->assign('mainFormItems',$formParams['mainFormItems']);
        $smarty->assign('columnsSon',$formParams['columnsSon']);
        $smarty->assign('sonKey','ExtendAttr');
        $smarty->assign('rules',$formParams['rules']);
        $smarty->assign('formTitle','基本信息');
        //是否隐藏子表表头中的新增按钮,可以不设置-默认为false,
        $smarty->assign('hideButtonAddSon',true);
        // $smarty->assign('urlRemoveSon',$this->_url('RemoveSonAjax'));
        $smarty->assign('sonButtons',array(
            array('text'=>'修改','type'=>'edit'),
            // array('text'=>'删除','type'=>'remove','options'=>array(
            //     'funcName'=>'handleDelete',
            //     'url'=>$this->_url('RemoveSonAjax'),
            //     //如果子表记录中存在__url字段,则会将$son['__url']作为ajax提交地址
            //     //适用于每行子表记录对应不同的ajax地址的场景
            //     'urlColumn'=>'__url',
            // )),
        ));
        $smarty->assign('row',$arr);
        $smarty->assign('action',$this->_url('SaveSetting'));
        $smarty->display('MainSonForm.tpl');
    }

    /**
     * 保存配置
     * Time：2019/10/22 15:54:07
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionSaveSetting(){
        $auth = $this->authCheck(0 ,true);
        if(!$auth){
            $ret = array('msg'=>'登录权限过期','success'=>false);
            echo json_encode($ret);exit;
        }

        //变量
        $modelProduct = FLEA::getSingleton("Model_Jichu_Product");
        $post = $this->axiosPost();
        if(!$post['id']){
            $ret = array('msg'=>'参数不完整，请重新设置','success'=>false);
            echo json_encode($ret);exit;
        }


        //如果已经有产品数据了，禁止修改属性值
        $_tmpCount = $modelProduct->findCount(array('kindId'=>$post['id']));
        if($_tmpCount > 0){
            // $ret = array('msg'=>'该类别已经有档案数据，禁止修改','success'=>false);
            // echo json_encode($ret);exit;
        }

        //扩展属性
        $ExtendAttr = array();
        foreach ($post['ExtendAttr'] as $k => & $v) {
            // if($v['title']){
                $ExtendAttr[] = array(
                    'type'     =>$v['type'],
                    'status'   =>$v['status'],
                    'isRequire'=>$v['isRequire'],
                    'title'    =>$v['title'],
                    'titleEng' =>$v['titleEng'],
                    'showType' =>$v['showType'],
                    'showSort' =>intval($v['showSort']),
                    'colWidth' =>$v['colWidth'] ? round($v['colWidth'],1) : 1,
                    'filed'    =>'filed'.($k+1),
                );

                //准备建索引
                if($v['type'] == 'autocomplete'){
                    $fileds_index[] = 'filed'.($k+1);
                }
            // }
        }
        // dump($post);exit;
        unset($post['ExtendAttr']);
        $post['extendAttr'] = json_encode($ExtendAttr);
        $data = array(
            'id'          =>$post['id'],
            'extendAttr'  =>$post['extendAttr'],
        );
        $result = $this->_modelExample->save($data);
        if($result){
            //准备建索引
            // foreach ($fileds_index as $key => & $index) {
            //     $sql = "SHOW INDEX FROM jichu_product where Column_name='{$index}'";
            //     $_ind = $this->_modelExample->findBySql($sql);
            //     // dump($sql);
            //     if($_ind[0]['Column_name'] == $index){
            //         //如果所以待删除的情况未考虑
            //     }else{
            //         $sql =  "ALTER TABLE `jichu_product` ADD INDEX `{$index}`(`{$index}`)";
            //         $res = $this->_modelExample->execute($sql);
            //     }
            // }
            $ret = array(
                'success'   =>true,
                'msg'       =>'操作成功',
            );
        }else{
            $ret = array(
                'success' =>false,
                'msg'     =>'操作失败',
            );
        }

        echo json_encode($ret);exit;
    }


    /**
     * 标签打印的配置
     * Time：2019/10/23 14:08:29
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionSetPrintConfig(){
        $this->authCheck($this->funcPrintId);

        $searchItems = array(
            'key'        =>''
        );
        $smarty = & $this->_getView();
        $arrFieldInfo = array(
            "id"          =>array('text'=>"编号",'width'=>'120'),
            "printName"    =>array('text'=>"模版名称",'width'=>''),
            "kindName"    =>array('text'=>"产品类别名称",'width'=>''),
            "extendAttr"  =>array('text'=>"扩展属性",'width'=>''),
            "printConfig" =>array('text'=>"是否设置",'width'=>''),
        );

        if(!FLEA::getAppInf('setAttrBySelf')){
            unset($arrFieldInfo['extendAttr']);
        }

        $smarty->assign('title', '产品类别列表');
        $smarty->assign('arr_field_info', $arrFieldInfo);
        $smarty->assign('action', $this->_url('GetRowsPrintConfig'));
        $smarty->assign('addUrl',$this->_url('AddPrintSenior'));
        $smarty->assign('searchItems', $searchItems);
        $smarty->assign('colsForKey', array(
                array('text' =>'关键字','col'=>'key')
        ));

        // $editButtons[] = array('text'=>'设置标签打印的配置信息','type'=>'redirect','icon'=>'el-icon-setting','options'=>array(
        //         //点击后跳转的地址
        //         'url'            =>$this->_url('SetPrint').'&id={id}',
        //         'disabledColumn' =>'__disabledSetPrint',
        //     ));
        // $editButtons[] = array('text'=>'高级设置','type'=>'redirect','icon'=>'el-icon-magic-stick','options'=>array(
        //         //点击后跳转的地址
        //         'url'            =>$this->_url('SetPrintSenior').'&id={id}',
        //         'disabledColumn' =>'__disabledSetPrint2',
        //     ));
        $editButtons = array(
            array('text'=>'编辑','type'=>'redirect','icon'=>'el-icon-edit','options'=>array(
                //点击后跳转的地址
                'url'            =>$this->_url('SetPrintSenior').'&id={id}',
                'disabledColumn' =>'__disabledEdit',
            )),
            array('text'=>'删除','type'=>'remove','options'=>array(
                'url'            =>$this->_url('RemovePrintConfigAjax'),
                'disabledColumn' =>'__disabledRemove',
            )),
            array('text'=>'复用','type'=>'redirect','icon'=>'el-icon-tickets','options'=>array(
                //点击后跳转的地址
                'url'            =>$this->_url('SetPrintSenior').'&id={id}&type=1',
                'disabledColumn' =>'__disabledFuyong',
            )),
        );
        $smarty->assign('editButtons',$editButtons);
        $smarty->display('TableList.tpl');
    }

    //配置打印的相关信息
    function buildHtmlPrint($kindId){

        $_options = array();
        $fileds = $this->_modelExample->getFileds($kindId ,'all');

        //选项处理
        $_options[] = array('text'=>'产品编号','value'=>'proCode');
        $_options[] = array('text'=>'产品名称','value'=>'proName');
        $_options[] = array('text'=>'入库登记的"花型颜色"','value'=>'proDesc');
        $_options[] = array('text'=>'入库登记的"格型"','value'=>'gexing');
        $_options[] = array('text'=>'入库登记的"组织"','value'=>'zuzhi');
        $_options[] = array('text'=>'入库登记的"后整理"','value'=>'hzl');
        $_options[] = array('text'=>'库位','value'=>'kuweiName');
        foreach ($fileds as & $v) {
            $_options[] = array('text'=>$v['title'] ,'value'=>$v['filed']);
        }
        // dump($_options);exit;

        $params = array();
        $params['formItems'] = array(
            array(
                'type'      =>'comp-select',
                'name'      =>'template',
                'title'     =>'标签模板',
                'clearable' =>true,
                'disabled'  =>false,
                'options'   =>array(
                    array('text'=>'二维码','value'=>'qrcode'),
                    array('text'=>'条形码','value'=>'barcode'),
                ),
                'value'     =>'',
            ),
            array(
                'type'      =>'comp-select',
                'name'      =>'language',
                'title'     =>'字段语种',
                'clearable' =>true,
                'disabled'  =>false,
                'options'   =>array(
                    array('text'=>'中文','value'=>'chinese'),
                    array('text'=>'英文','value'=>'english'),
                    array('text'=>'中文+英文','value'=>'chineseAndEnglish'),
                ),
                'value'     =>'',
            ),
            array(
                'type'      =>'comp-select',
                'name'      =>'fontSize',
                'title'     =>'字体大小',
                'clearable' =>true,
                'disabled'  =>false,
                'options'   =>array(
                    array('text'=>'9号','value'=>'9'),
                    array('text'=>'10号','value'=>'10'),
                    array('text'=>'11号','value'=>'11'),
                    array('text'=>'12号','value'=>'12'),
                    array('text'=>'13号','value'=>'13'),
                    array('text'=>'14号','value'=>'14'),
                    array('text'=>'15号','value'=>'15'),
                ),
                'value'     =>'',
            ),
            array(
                'type'      =>'comp-text',
                'name'      =>'pageWidth',
                'title'     =>'纸张宽度',
                'clearable' =>true,
                'value'     =>'',
                'addonEnd'  =>'mm',
            ),
            array(
                'type'      =>'comp-text',
                'name'      =>'pageHeight',
                'title'     =>'纸张高度',
                'clearable' =>true,
                'value'     =>'',
                'addonEnd'  =>'mm',
            ),
            array(
                'type'      =>'comp-text',
                'name'      =>'width',
                'title'     =>'左侧标题',
                'clearable' =>true,
                'value'     =>'',
                'addonEnd'  =>'标题宽度mm，仅条码模版生效',
            ),
            array(
                'type'      =>'comp-select',
                'name'      =>'fileds',
                'title'     =>'标签字段',
                'clearable' =>true,
                'disabled'  =>false,
                'multiple'  =>true,
                'options'   =>$_options,
                'value'     =>array(),
            ),
            array(
                'type'         =>'comp-image',
                'name'         =>'logo',
                'title'        =>'Logo',
                'action'       =>$this->_url('UploadImage'),//上传地址
                'actionRemove' =>$this->_url('RemoveImage'),//删除图片时需要从服务器删除,可以不定义
                'accept'       =>'.jpg,.png',//接受上传的文件类型
                'limit'        =>1,//最大允许上传个数
                'multiple'     =>false,//是否允许多选,
            )
        );

        $params['rules'] = array(
            'template'=>array(
                array(
                    'required'=>true,
                    'message'=>'模板必须',
                )
            ),
            'language'=>array(
                array(
                    'required'=>true,
                    'message'=>'语言必须',
                )
            ),
            'pageWidth'=>array(
                array(
                    'required'=>true,
                    'message'=>'纸张宽度必须',
                )
            ),
            'pageHeight'=>array(
                array(
                    'required'=>true,
                    'message'=>'纸张高度必须',
                )
            ),
            'fontSize'=>array(
                array(
                    'required'=>true,
                    'message'=>'字体大小必须',
                )
            ),
            'width'=>array(
                array(
                    'required'=>true,
                    'message'=>'左侧宽度必须',
                )
            ),
            'fileds'=>array(
                array(
                    'required'=>true,
                    'message'=>'打印字段必须',
                )
            ),
        );

        return $params;
    }

    /**
     * 配置打印的页面
     * Time：2019/10/23 14:30:50
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionSetPrint(){
        $arr = $this->_modelExample->find($_GET['id']);
        $formParams = $this->buildHtmlPrint($_GET['id']);

        $arr['printConfig'] && $_config = json_decode($arr['printConfig'] ,1);
        $_config['id'] = $arr['id'];
        // dump($_config);exit;

        $smarty = & $this->_getView();

        $smarty->assign('formItems',$formParams['formItems']);
        $smarty->assign('rules',$formParams['rules']);
        $smarty->assign('title','基本信息');
        $smarty->assign('row',$_config);
        $smarty->assign('action',$this->_url('SavePrint'));
        $smarty->display('MainForm.tpl');
    }

    /**
     * 保存
     * Time：2018/12/17 13:41:52
     * @author li
    */
    function actionSavePrint() {
        $auth = $this->authCheck(0 ,true);
        if(!$auth){
            $ret = array('msg'=>'登录权限过期','success'=>false);
            echo json_encode($ret);exit;
        }

        //变量
        $modelProduct = FLEA::getSingleton("Model_Jichu_Product");
        $post = $this->axiosPost();
        // dump($post);
        if(!$post['id']){
            $ret = array('msg'=>'参数不完整，请重新设置','success'=>false);
            echo json_encode($ret);exit;
        }

        $post['printConfig'] = array(
            'template'   =>$post['template'],
            'language'   =>$post['language'],
            'fontSize'   =>$post['fontSize'],
            'pageWidth'  =>$post['pageWidth'],
            'pageHeight' =>$post['pageHeight'],
            'width'      =>$post['width'],
            'fileds'     =>$post['fileds'],
        );
        // dump($post['printConfig']);
        $count = count(array_filter($post['printConfig']));
        if(count($post['printConfig']) != $count){
            $ret = array('msg'=>'参数配置不完整，请全部设置','success'=>false);
            echo json_encode($ret);exit;
        }

        $post['printConfig']['logo']   = $post['logo'];
        $post['printConfig']['Senior'] = '';

        $data = array(
            'id'          =>$post['id'],
            'printConfig' =>json_encode($post['printConfig']),
        );
        // dump($post);exit;

        $result = $this->_modelExample->save($data);
        if($result){
            $ret = array(
                'success' =>true,
                'msg'     =>'操作成功',
            );
        }else{
            $ret = array(
                'success' =>false,
                'msg'     =>'操作失败',
            );
        }

        echo json_encode($ret);exit;
    }

    /**
     * 配置打印的页面
     * Time：2019/10/23 14:30:50
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionSetPrintSenior(){
        $Arr = $this->_modelPrintConfig->find($_GET['id']);
        //查打印设置
        $fileds = $this->getPrintFileds($Arr['kindId']);
        $objectData = $fileds['objectData'];
        $Arr['printConfig'] = $Arr['printConfig'] ? json_decode($Arr['printConfig'],true) : '';
        $Arr['compName'] = $this->getCompName();
        $Arr['code'] = "我是测试code内容";
        $prokind = $this->_modelExample->getOptions();
        $Arr['printConfig']['filedList'] = $fileds['filedList'];
        if($_GET['type']){
            unset($Arr['id']);
        }
        $smarty = & $this->_getView();
        $smarty->assign('title','打印高级配置');
        $smarty->assign('row',$Arr);
        $smarty->assign('prokind',$prokind);
        $smarty->assign('type','1');
        $smarty->assign('objectData',$objectData);
        $smarty->display('Jichu/ProKind/setPrint.tpl');
    }

    /**
     * 新增打印模板
     * Time：2021年3月3日 16:06:52
     * @author lxd
     * @param 参数类型
     * @return 返回值类型
    */
    function actionAddPrintSenior(){
        $prokind = $this->_modelExample->getOptions();
        $row = array('printName'=>'','printConfig'=>array('Senior'=>''));
        $smarty = & $this->_getView();
        $smarty->assign('title','打印高级配置');
        $smarty->assign('row',$row);
        $smarty->assign('prokind',$prokind);
        $smarty->display('Jichu/ProKind/setPrint.tpl');
    }

    // 保存打印
    function actionSaveSetPrintSenior(){
        $id = intval($_POST['id']);
        // if(!$_POST['scode']){
        //     echo json_encode(array('msg'=>'缺少排版数据','success'=>false));exit;
        // }
        //赋值
        $arr['fontSize'] = $_POST['object']['fontSize'];
        $arr['pageHeight'] = $_POST['object']['height'];
        $arr['pageWidth'] = $_POST['object']['width'];
        $arr['width'] = '15';
        $arr['printName'] = $_POST['object']['printName'];
        $arr['language'] = 'chinese';
        $arr['pic']['url'] = $_POST['object']['pic'];
        $arr['template'] = 'qrcode';
        $arr['fileds'] = $_POST['object']['printConfig']['fileds'];
        // $arr['printConfig'] = $arr;
        $arr['Senior'] = $_POST['scode'].'';
        // dump($arr);exit;
        if($_POST['object']['isDefault'] == '1'){
            $sql = "update jichu_prokind_print_config set isDefault = '0' where kindId = '{$_POST['object']['kindId']}'";
            $tmp = $this->_modelPrintConfig->execute($sql);
        }
        $data = array(
            'id'          =>$id,
            'kindId'      =>$_POST['object']['kindId'],
            'isDefault'     =>$_POST['object']['isDefault'],
            'printConfig' =>json_encode($arr),
        );
        // dump($data);exit;
        $result = $this->_modelPrintConfig->save($data);
        echo json_encode(array('success'=>$result));exit;
    }

    /**
     * ps ：
     * Time：2019/11/11 14:53:04
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionCopyKind(){
        $post = $this->axiosPost();
        $id = intval($post['id']);

        $row = $this->_modelExample->find($id);
        // dump($row);exit;
        $row['id'] = 0;
        $row['kindName'] .= "(1)";

        $res = $this->_modelExample->create($row);
        echo json_encode(array('msg'=>'复制完成','success'=>true));
    }

    /**
     * @desc ：图片上传
     * Time：2017/08/03 10:19:56
     * @author Wuyou
    */
    function actionAddPics(){
        //禁止上传的文件类型
        $upBitType = array(
            'application/x-msdownload',//exe,dll
            'application/octet-stream',//bat
            'application/javascript',//js
            'application/msword',//word
            'application/vnd.ms-excel',//word
        );
        if($_FILES['file']['name']!=''){
            //附件大小不能超过5M
            $max_size=5;//M
            $max_size2=$max_size*1024*1024;
            if($_FILES['file']['size'][0]>$max_size2){
                // return array('success'=>false,'msg'=>"附件上传失败，请上传小于{$max_size}M的附件");
                echo json_encode(array('success'=>false,'msg'=>"附件上传失败，请上传小于{$max_size}M的附件"));exit;
            }

            //限制类型
            if(in_array($_FILES['file']['type'][0],$upBitType)){
                // return array('success'=>false,'msg'=>"该文件类型不允许上传");
                echo json_encode(array('success'=>false,'msg'=>'允许上传bmp,jpg,png,gif,该文件类型不允许上传'));exit;
            }
            // dump($_FILES);die;

            // $path = "upload/Order/"; //图片保存路径
            $path="upload/Images/";
            //定义大图和小图的尺寸
            $size=GetImageSize($_FILES['file']['tmp_name']);
            // dump($size);exit;
            $h_default=400;
            if($size[1]>$h_default){
                $width =$h_default/$size[1]*$size[0];
                $height=$h_default;
            }else{
                $width=$size[0];
                $height=$size[1];
            }
            $size = array(
                'big'=>array($width,$height),
                'small'=>array(60,60)
            );
            FLEA::loadClass('FLEA_Helper_Image');
            $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
            $nu = date('ymdHis');
            $bfileName = $path.'b'.$nu.'.'.$ext;//创建大图文件名和路径
            $sfileName = $path.'s'.$nu.'.'.$ext;    //创建小图文件名和路径

            $img =& FLEA_Helper_Image::createFromFile($_FILES['file']['tmp_name'], $ext);//不能使用getSingleton创建对象，必须有图像实体
            $img->resampled($size['big'][0],$size['big'][1]);

            $img->saveAsJpeg($bfileName,100);

            $img->resize($size['small'][0],$size['small'][1]);
            $img->saveAsJpeg($sfileName,100);
        }

        echo json_encode(array('success'=>true,'bpic'=>$bfileName,'spic'=>$sfileName,'class'=>$nu,'msg'=>'上传成功！'));exit;

    }

    //产品大类
    function actionGetOptions(){
        $str="select * from jichu_prokind";
        $row = $this->_modelExample->findBySql($str);
        foreach($row as & $v){
            $arr[]=array('value'=>$v['id'],'text'=>$v['kindName']);
        }
        $arr = json_encode($arr);
        return $arr;
    }

    // 所有的打印科目名称
    function getPrintFileds($kindId = ''){
        $arr = $this->_modelExample->find($kindId);
        $arr['extendAttr'] && $fields = json_decode($arr['extendAttr'] ,1);
        // dump($fields);exit;

        $extendAttr = array();
        //组织扩展字段
        foreach ($fields as $k => & $v) {
            if($v['status'] == 'off'){
                continue;
            }
            $extendArr['filed'][] = $v['filed'];
        }
        //标题映射
        $titleConfig = array(
            'chinese'           =>'title',
            'english'           =>'titleEng',
            'chineseAndEnglish' =>'title,titleEng',
        );

        //花型颜色的标题
        $_tR = array(
            'proDesc'=>array(
                'chinese'           =>'颜色',
                'english'           =>'Detail',
                'chineseAndEnglish' =>'颜色/Detail',
            ),
            'gexing'=>array(
                'chinese'           =>'格型',
                'english'           =>'Pattern',
                'chineseAndEnglish' =>'格型/Pattern',
            ),
            'zuzhi'=>array(
                'chinese'           =>'组织',
                'english'           =>'Construction',
                'chineseAndEnglish' =>'组织/Construction',
            ),
            'hzl'=>array(
                'chinese'           =>'后整理',
                'english'           =>'Finish',
                'chineseAndEnglish' =>'后整理/Finish',
            ),
            'kuweiName'=>array(
                'chinese'           =>'库位',
                'english'           =>'Location',
                'chineseAndEnglish' =>'库位/Location',
            ),
            'date'=>array(
                'chinese'           =>'日期',
                'english'           =>'Date',
                'chineseAndEnglish' =>'日期/Date',
            ),
            'remark'=>array(
                'chinese'           =>'备注',
                'english'           =>'Remark',
                'chineseAndEnglish' =>'备注/Remark',
            ),
            'code'=>array(
                'chinese'           =>'条码',
                'english'           =>'code',
                'chineseAndEnglish' =>'条码/code',
            ),
        );

        $_tP = array(
            'proName'=>array(
                'chinese'           =>'名称',
                'english'           =>'Name',
                'chineseAndEnglish' =>'名称/Name',
            ),
            'proCode'=>array(
                'chinese'           =>'编号',
                'english'           =>'No.',
                'chineseAndEnglish' =>'编号/No.',
            ),
        );

        $_detialTitle = array_merge($_tR ,$_tP);
        $baseArr = array();
        foreach ($_detialTitle as $k => & $v) {
            $baseArr['filed'][] = $k;
        }
        $_config['fileds'] = array_merge($baseArr['filed'],$extendArr['filed']);
        $_config['language'] = 'chinese';
        $_config['id'] = $arr['id'];
        foreach ($fields as & $f) {
            if($f['status'] == 'off'){
                continue;
            }

            $tmp = array();
            $tmp[] = $f['title'];
            $f['titleEng'] && $tmp[] = $f['titleEng'];
            $f['chinese'] = $f['title'];
            $f['english'] = $f['titleEng'] ? $f['titleEng'] : '未配置';
            $f['chineseAndEnglish'] = join('/' ,$tmp);

            $_detialTitle[$f['filed']] = array(
                'chinese'           =>$f['chinese'],
                'english'           =>$f['english'],
                'chineseAndEnglish' =>$f['chineseAndEnglish'],
            );
        }

        // dump($fields);
        foreach ($_config['fileds'] as $key => & $v) {
            $_config['filedList'][$v] = array(
                'filed'=>$v,
                'title'=>$_detialTitle[$v][$_config['language']],
                // 'value'=>$_detialTitle[$v][$_config['language']].'的值',
            );

            $_config['objectData'][$v] = $_detialTitle[$v][$_config['language']].'的值';
        }
        $_config['objectData']['code'] = "我是测试code内容";
        $_config['compName'] = $this->getCompName();
        return $_config;
    }

    //产品其他属性
    function actionGetExtendArr(){
        $_config = $this->getPrintFileds($_POST['id']);
        echo json_encode(array('success'=>true,'config'=>$_config,'msg'=>'获取成功'));exit;

    }

    /**
     * 获取所有打印配置页
     * @author lxd
    */
    public function actionGetRowsPrintConfig()
    {

        $auth = $this->authCheck(0 ,true);

        if(!$auth){
            $ret = array(
              'total'   =>0,
              'columns' =>array(),
              'rows'    =>array(),
            );
            echo json_encode($ret);exit;
        }

        $post = $this->axiosPost();

        $pagesize    = $post['pagesize'];
        $currentPage = $post['currentPage'];

        $keyField    = isset($post['colForKey']) ? $post['colForKey'] : 'key';
        //处理搜索
        $post[$keyField] = $post['key'];
        if($keyField!='key') $post['key'] = '';

        $arr = $post;

        $sql = "SELECT x.*,y.kindName,y.extendAttr FROM jichu_prokind_print_config x left join jichu_prokind y on x.kindId = y.id
                where 1";
        if($arr['key']!='') {
            $sql .= " and y.kindName like '%{$arr['key']}%'";
        }

        // dump($sql);exit;
        FLEA::loadClass('TMIS_Pager');
        $pager = new TMIS_Pager($sql,null,null,$pagesize ,($currentPage - 1));
        $rowset = $pager->findAll();
        // dump($rowset);exit;
        $modelProduct = FLEA::getSingleton("Model_Jichu_Product");
        foreach($rowset as & $v){
            //如果已经有产品数据了，禁止修改属性值
            // $_tmpCount = $modelProduct->findCount(array('kindId'=>$v['id']));
            // $_tmpCount > 0 && $v['__disabledSetting'] = true;

            if($v['extendAttr']){
                $_tmp = json_decode($v['extendAttr'] ,1);
                $_tmpExt = array();
                foreach ($_tmp as & $f) {
                    if($f['title'] && $f['status'] == 'on'){
                        $_tmpExt[] = $f['title'];
                    }
                }
                $v['extendAttr'] = join(',',$_tmpExt);
            }

            //判断是否有配置打印标签的设置
            if($v['printConfig']){
                $_tmp = json_decode($v['printConfig'] ,1);
                if($_tmp['template']){
                    $v['printConfig'] = "已设置";
                }
                $v['printName'] = $_tmp['printName'];

                if($_tmp['template'] != 'qrcode'){
                    $v['__disabledSetPrint2'] = true;
                }
            }
        }

        $ret = array(
          'total'   =>$pager->totalCount,
          'columns' =>array(),
          'rows'    =>$rowset,
        );
        echo json_encode($ret);exit;
    }
}
?>